package com.weaver.qfengx.service.workflow;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.workflow.biz.freeNode.FreeNodeBiz;
import com.engine.workflow.entity.publicApi.PAResponseEntity;
import com.engine.workflow.entity.publicApi.ReqOperateRequestEntity;
import com.engine.workflow.entity.publicApi.WorkflowDetailTableInfoEntity;
import com.engine.workflow.publicApi.WorkflowRequestOperatePA;
import com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl;
import com.google.common.collect.Maps;
import com.weaver.qfengx.NumberUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;
import weaver.workflow.webservices.WorkflowRequestTableField;

import java.util.List;
import java.util.Map;

/**
 * 封装流程相关接口
 */
public class WorkflowService {

    private static WorkflowRequestOperatePA operatePA = ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
    private final static Logger log = LoggerFactory.getLogger(WorkflowService.class);

    /**
     * 删除流程
     *
     * @param requestid
     * @return
     */
    public static PAResponseEntity deleteWorkflow(String requestid, User user, int ismonitor) {
        ReqOperateRequestEntity operateRequestEntity = new ReqOperateRequestEntity();
        operateRequestEntity.setRequestId(NumberUtils.parseInt(requestid));
        Map<String, Object> otherParams = Maps.newHashMap();
        otherParams.put("ismonitor", ismonitor);
        operateRequestEntity.setOtherParams(otherParams);
        PAResponseEntity paResponseEntity = operatePA.deleteRequest(user, operateRequestEntity);
        return paResponseEntity;
    }

    /**
     * 保存流程
     *
     * @return
     */
    public static PAResponseEntity saveWorkflow(User user, Map<String, String> data) {
        PAResponseEntity paResponseEntity = operatePA.doCreateRequest(user, map2Entity(data));
        return paResponseEntity;
    }

    /**
     * map 对象转成ReqOperateRequestEntity
     *
     * @param data
     */
    private static ReqOperateRequestEntity map2Entity(Map<String, String> data) {
        ReqOperateRequestEntity operateRequestEntity = new ReqOperateRequestEntity();
        try {
            int workflowId = Util.getIntValue(data.get("workflowId"));
            int requestId = Util.getIntValue(data.get("requestId"));
            String requestName = Util.null2String(data.get("requestName"));
            int userId = Util.getIntValue(data.get("userId"));
            int forwardFlag = Util.getIntValue(data.get("forwardFlag"));
            String forwardResourceIds = Util.null2String(data.get("forwardResourceIds"));
            String mainDataStr = Util.null2String(data.get("mainData"));
            if (!"".equals(mainDataStr)) {
                try {
                    List<WorkflowRequestTableField> mainData = JSONObject.parseArray(mainDataStr, WorkflowRequestTableField.class);
                    operateRequestEntity.setMainData(mainData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            String detailDataStr = Util.null2String(data.get("detailData"));
            if (!"".equals(detailDataStr)) {
                try {
                    List<WorkflowDetailTableInfoEntity> detailData = JSONObject.parseArray(detailDataStr, WorkflowDetailTableInfoEntity.class);
                    operateRequestEntity.setDetailData(detailData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            String remark = Util.null2String(data.get("remark"));
            String requestLevel = Util.null2String(data.get("requestLevel"));
            String otherParamStr = Util.null2String(data.get("otherParams"));
            if (!"".equals(otherParamStr)) {
                try {
                    Map<String, Object> otherParams = JSONObject.parseObject(otherParamStr, Map.class);
                    operateRequestEntity.setOtherParams(otherParams);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            operateRequestEntity.setWorkflowId(workflowId);
            operateRequestEntity.setRequestId(requestId);
            operateRequestEntity.setRequestName(requestName);
            operateRequestEntity.setUserId(userId);
            operateRequestEntity.setRemark(remark);
            operateRequestEntity.setRequestLevel(requestLevel);
            operateRequestEntity.setForwardFlag(forwardFlag);
            operateRequestEntity.setForwardResourceIds(forwardResourceIds);
//            operateRequestEntity.setClientIp(request.getRemoteAddr());
            //干预参数
            int submitNodeId = Util.getIntValue(Util.null2String(data.get("submitNodeId")));
            if (submitNodeId > 0 || FreeNodeBiz.isFreeNode(submitNodeId)) {
                operateRequestEntity.setSubmitNodeId(submitNodeId);
                operateRequestEntity.setEnableIntervenor("1".equals(Util.null2s(data.get("enableIntervenor"), "1")));
                operateRequestEntity.setSignType(Util.getIntValue(Util.null2String(data.get("SignType")), 0));
                operateRequestEntity.setIntervenorid(Util.null2String(data.get("Intervenorid")));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }
        return operateRequestEntity;
    }

}
